package com.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.net.URI;

public class PatentCount extends Configured implements Tool {
    static class PatentCountMapper extends
            Mapper<LongWritable, Text, Text, IntWritable>{
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
//            key:k1(偏移量)			value:v1(数据)
//0					694420,826965
//...					586678,734541
//					841432,979709
//					220864,880384
//....					326297,846978
            String[] datas = value.toString().split(",");
//            datas:["326297","846978"]
            String k2 = datas[1];
            int v2 = 1;
            context.write(new Text(k2),new IntWritable(v2));
//...
//->map(求被引用的专利编号出现次数)
//k2				v2
//826965			1
//734541			1
//979709			1
//880384			1
//846978			1
        }
    }
    static class PatentCountReducer extends
            Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
//             key:k2                  values: v2
//            826965			[1,1]
//734541			[1]
//979709			[1]
//880384			[1]
//846978			[1]
//->reduce
            int sum = 0;
            for (IntWritable val:values){
                sum += val.get();
            }
            System.out.println(sum);
            context.write(key,new IntWritable(sum));
//k3			      v3
//826965			2
//734541			1
//979709			1
//880384			1
//846978			1
        }
    }

    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();
        // 指定输入输出路径
        Path input = new Path(
                "hdfs://192.168.10.11:9000/patentdata");
        Path output = new Path(
                "hdfs://192.168.10.11:9000/patentoutput");

        FileSystem fs = FileSystem.get(
                new URI("hdfs://192.168.10.11:9000")
                ,conf);
        if (fs.exists(output)) fs.delete(output,true);
        //构建Job
        Job job = Job.getInstance(conf);
        job.setJobName("patent");
        job.setJarByClass(this.getClass());
        // 指定map类的配置
        job.setMapperClass(PatentCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        // 指定reduce类的配置
        job.setReducerClass(PatentCountReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 指定输入处理器
//        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job,input);
        // 指定输出处理器
//        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job,output);

        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(ToolRunner.run(new PatentCount(),args));
    }
}
